const App = getApp();
Page({
  data: {
    name: '',
    mobile: '',
    address:'',
    type_ids: '',
    type_list: [],
    order_id: 0,
    img_array_id: [],
    img_list: [],
    card_zheng: '/images/card_zheng.png',
    zheng_id:'',
    card_fan: '/images/card_fan.png',
    fan_id:''
  },
  YhkName: function (e) {
    var value = e.detail.value
    this.setData({
      name: value
    })
  },
  uploadImage: function () {
    // var order_id = this.data.order_id
    var that = this
    wx.chooseImage({
      count: 9,
      success(res) {
        const tempFilePaths = res.tempFilePaths
        var img_list = that.data.img_list
        var img_array_id = that.data.img_array_id
        for (let i = 0; i < tempFilePaths.length; i++) {
          wx.uploadFile({
            url: App.api_root + 'upload/image',
            filePath: tempFilePaths[i],
            name: 'iFile',
            formData: {
              // order_id: order_id,
              image_ids: tempFilePaths[i],
              wxapp_id: App.getWxappId(),
              token: App.getToken()
            },
            success(res) {
              var json = JSON.parse(res.data)
              var img = json.data.file_path
              var id = json.data.file_id
              img_list.push(img)
              img_array_id.push(id)
              that.setData({
                img_list: img_list,
                img_array_id: img_array_id
              })
            }
          })
        }
      }
    })
  },
  DelImage: function (e) {
    var id = e.currentTarget.dataset.id
    var img = e.currentTarget.id
    var img_list = this.data.img_list
    var img_array_id = this.data.img_array_id
    img_list.splice(img_list.indexOf(img), 1)
    img_array_id.splice(img_array_id.indexOf(id), 1)
    this.setData({
      img_list: img_list,
      img_array_id: img_array_id
    })
  },
  upload_zheng:function(){
    this.uploadImg('card_zheng','zheng_id')
  },
  upload_fan:function(){
    this.uploadImg('card_fan','fan_id')
  },
  uploadImg: function (card,id) {
    var that = this
    wx.chooseImage({
      count: 1,
      success(res) {
        const tempFilePaths = res.tempFilePaths
        var imgUrl = ''
        var imgId = ''
        
        for (let i = 0; i < tempFilePaths.length; i++) {
          wx.uploadFile({
            url: App.api_root + 'upload/image',
            filePath: tempFilePaths[0],
            name: 'iFile',
            formData: {
              // order_id: order_id,
              image_ids: tempFilePaths[0],
              wxapp_id: App.getWxappId(),
              token: App.getToken()
            },
            success(res) {
              var json = JSON.parse(res.data)
              var img = json.data.file_path
              var id = json.data.file_id
              imgUrl = img
              imgId = id
              // img_list.push(img)
              // img_array_id.push(id)
              if(card == 'card_zheng'){
                that.setData({
                  card_zheng: imgUrl,
                  zheng_id: imgId
                })
              }else if(card == 'card_fan'){
                that.setData({
                  card_fan: imgUrl,
                  fan_id: imgId
                })
              }
              
            }
          })
        }
      }
    })
  },
  YhkKhh: function (e) {
    var value = e.detail.value
    this.setData({
      mobile: value
    })
  },
  YhkAdd: function (e) {
    var value = e.detail.value
    this.setData({
      address: value
    })
  },
  checkboxChange: function (e) {
    var value = e.detail.value
    var type_ids = ''
    for (var i = 0; i < value.length; i++) {
      if (i == 0) {
        type_ids = value[i]
      } else {
        type_ids = type_ids + ',' + value[i]
      }
    }
    this.setData({
      type_ids: type_ids
    })
  },
  TijiaoSq: function () {
    var name = this.data.name
    if (!name) {
      wx.showModal({
        title: '温馨提示：',
        content: '请输入姓名',
        showCancel: false,
        confirmText: '关闭提示',
        success(res) {

        }
      })
      return false
    }
    var mobile = this.data.mobile
    if (!mobile) {
      wx.showModal({
        title: '温馨提示：',
        content: '请输入手机',
        showCancel: false,
        confirmText: '关闭提示',
        success(res) {

        }
      })
      return false
    }
    if (mobile.length != 11) {
      wx.showModal({
        title: '温馨提示：',
        content: '手机格式错误',
        showCancel: false,
        confirmText: '关闭提示',
        success(res) {

        }
      })
      return false
    }

    var address = this.data.address
    if (!address) {
      wx.showModal({
        title: '温馨提示：',
        content: '请输入地址',
        showCancel: false,
        confirmText: '关闭提示',
        success(res) {

        }
      })
      return false
    }

    


    var type_ids = this.data.type_ids
    if (!type_ids) {
      wx.showModal({
        title: '温馨提示：',
        content: '请选择行业',
        showCancel: false,
        confirmText: '关闭提示',
        success(res) {

        }
      })
      return false
    }

    var man_front = this.data.zheng_id
    if(!man_front){
      wx.showModal({
        title: '温馨提示：',
        content: '请上传身份证正面',
        showCancel: false,
        confirmText: '关闭提示',
        success(res) {

        }
      })
      return false
    }
    var man_back = this.data.fan_id
    if(!man_back){
      wx.showModal({
        title: '温馨提示：',
        content: '请上传身份证反面',
        showCancel: false,
        confirmText: '关闭提示',
        success(res) {

        }
      })
      return false
    }
    // if(this.data.zheng_id)
    var image_id = this.data.img_array_id
    var image_ids = ''

    console.log(this.data.img_list)
    
    var imglist = this.data.img_list

    for(let i = 0; i< imglist.length;i++){
      image_ids += imglist[i] + ','
     
    }
    image_ids = image_ids.slice(0,image_ids.length - 1)

    if(!image_ids){
      wx.showModal({
        title: '温馨提示：',
        content: '请上传个人近期生活照',
        showCancel: false,
        confirmText: '关闭提示',
        success(res) {

        }
      })
      return false
    }



    App._post_form2('user.server.apply/submit', {
      name: name,
      mobile: mobile,
      type_ids: type_ids,
      address:address,
      man_front:this.data.card_zheng,
      man_back:this.data.card_fan,
      image_ids:image_ids,
    }, function (result) {
      wx.showModal({
        title: '温馨提示：',
        content: '已成功发送申请',
        showCancel: false,
        confirmText: '关闭提示',
        success(res) {
          wx.navigateBack();
        }
      })
    });
  },
  onLoad: function (options) {
    var _this = this
    App._post_form2('service/serverTypeList', {}, function (result) {
      _this.setData({
        type_list: result.data
      })
    });
  },
  onReady: function () {

  },
  onShow: function () {

  },
  onHide: function () {

  },
  onUnload: function () {

  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})